package alexanderHash;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;

import jxl.JXLException;

import benchmarking.benchmarker;

public class Main {
    public static void main(String[] args) throws IOException, JXLException {
        if (args.length != 1) {
            System.err.println("Error: Incorrect number of arguments, expected one");
            System.exit(1);
        }

        // Create a new hash with the found array size
        DynamicHash dh = new DynamicHash();

        // �ben filen igen og begynd at tilf�je v�rdier.
        // Track the latest URL.
        BufferedReader infile = new BufferedReader(new FileReader(args[0]));
        String lineRead = infile.readLine();
        String lastURL = "";
        
        while(lineRead != null) {
            if((lineRead.length() >= 6 && lineRead.substring(0, 6).equals("*PAGE:")))
                lastURL = lineRead.substring(6);
            else
                dh.put(lineRead, lastURL);

            lineRead = infile.readLine();
        }
        
        // The number of elements in the hash is 2 because keys are
        // unique, i.e. it contains two keys: "ITU" and "Peter"
        System.out.println("Number of elements in hash: " + dh.noElements());

        // The load factor is the fraction between the elements stored in the hash
        // and the size of the array: 2 / 20 = 0.1
        System.out.println("Load factor               : " + dh.loadFactor());
        System.out.println("Dynamic hash resized " + dh.noResized() + " times\n");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Search for: ");
        
        //Changed for benchmarking. Org. lines with *
        //*lineRead = br.readLine();
        lineRead = "alfabetmandenik�benhavn";
        int i=1;
        String name ="large";
        
        benchmarker timer = new benchmarker();
        
        timer.createSheet(name);
        
       //*while (!lineRead.isEmpty()) {
        while (i<100) {	
        	
        	
        	timer.start();
            String urls = dh.getURLs(lineRead);
            timer.end();
            timer.toExel(name);
            if (urls.isEmpty())
                System.out.println("Unable to find word \"" + lineRead + "\"\n");
            else
                System.out.println(urls);

            System.out.print("Search for: ");
            //*lineRead = br.readLine();
            i++;
        }
        timer.close();
    }
}
